package _1_MQ.Kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

public class KafkaProducerTest{
    public static void main(String[] args){
        Properties props = new Properties();
        //连接Kafka的IP地址
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ProducerConfig.ACKS_CONFIG, "all");
        //key的序列号
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        //value的序列号
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");

        //话题
        String topic = "myTopic";
        String key;
        String value;

        Producer<String, String> producer = new KafkaProducer<>(props);
        for (int i = 20; i <= 30; i++){
            key = Integer.toString(i);
            value = "消息 - " + i;
            producer.send(new ProducerRecord<>(topic, key, value));
        }

        producer.close();

    }


}
